<template>
  <basic-container>
    <avue-crud
        v-if="tableOption"
        v-bind="bindVal"
        v-on="onEvent"
        v-model:page="page"
        v-model="startForm">
      <template #menu-left="{size}">
        <el-button type="primary" v-if="getBtnPermission('addNewBtn',null,null,false)" :size="size"
                   @click="startInstance">启动新实例
        </el-button>
      </template>
      <template #menu="scope">
        <el-button v-if="![-10, 20].includes(scope.row.procHandlerState)" :type="scope.type" text
                   :disabled="scope.disabled"
                   :size="scope.size"
                   @click="cancelActEvent(scope.row)">取消
        </el-button>
      </template>
    </avue-crud>
    <start-config-dialog ref="startConfigDialog" @refresh="refreshChange"></start-config-dialog>
  </basic-container>
</template>

<script>
import startConfigDialog from "./startConfigDialog.vue";
import {createStaticMixin} from "@/mixins/startCrud";
import autoRouter from "@/views/start/activiti/autoRouter";
import startActApi from "@/api/start/activiti/core/startActApi";
import actBusinessOption from "@/option/start/activiti/actBusinessOption";

const option = {
  option: actBusinessOption,
  api: startActApi,
  permissionBtnHandler: {
    delBtn: (vm, row, defaultValue) => {
      return [-10, 20].includes(row.procHandlerState)
    }
  }
}

export default {
  name: "index",
  mixins: [createStaticMixin(option)],
  components: {startConfigDialog},
  created() {
  },
  methods: {
    loadCrudOptionBefore(option) {
      option.addBtn = false;
      option.delBtn = true;
      option.viewBtn = true;
    },
    beforeOpenViewDialog(row) {
      let title = "流程业务详情【" + row.procDefName + "】" + row.procStartUserFinalName
      this.$router.push({path: autoRouter.paths.procDetailPath + "/" + row.id + "/" + title})
      return true;
    },
    cancelActEvent(row) {
      let option = {}
      let msgBoxDone
      option.beforeClose = function (action, instance, done) {
        msgBoxDone = done;
        if (action !== "confirm") {
          done();
          return
        }
        startActApi.cancelApply(row.id, instance.inputValue).then(res => {
          done();
        })
      }
      this.$prompt("系统提示", `您确定要取消【${row.procDefName}】此流程申请`, option).catch(e => {

      });
    },

    rowDel(row, index) {
      this.$prompt(`此操作将永久删除序号为【${index + 1}】的数据！ 请输入删除原因?`, '系统提示', {
        confirmButtonText: '是',
        cancelButtonText: '否',
        inputType: "textarea",
        type: 'warning',
        inputErrorMessage: "删除原因不能为空",
        inputValidator: function (value) {
          return !!value;
        }
      }).then(({value}) => {
        return this.api['deleteById'](row.id, {reason: value})
      }).then(res => {
        this.loadRemoteDataSources()
        this.$message.success('删除成功')
        if (this.rowDelAfter) this.rowDelAfter(row, index);
      })
    },
    startInstance() {
      this.$refs.startConfigDialog.show();
    }
  }
}
</script>

<style scoped>

</style>
